import {Sequelize, DataTypes} from 'sequelize';

const pgConfig = {
    host: "95.169.25.181",
    port: 5432,
    database: "dbmt2309",
    username: "postgres",
    password: "123"
};
// Create a Sequelize instance
const sequelize = new Sequelize(pgConfig.database, pgConfig.username, pgConfig.password, {
    host:pgConfig.host,
    dialect: 'postgres',
});

// Define a model
const User = sequelize.define('DiyUser', {
    firstName: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    lastName: {
        type: DataTypes.STRING,
        allowNull: false,
    },
});
async function main() {
    sequelize.sync()
        .catch((error) => {
            console.error('Error syncing database:', error);
        });

    const createUser = async (firstName, lastName) => {
        try {
            const newUser = await User.create({ firstName, lastName });
            console.log('User created:', newUser.toJSON());
        } catch (error) {
            console.error('Error creating user:', error);
        }
    };

// Example usage:
//     createUser('John', 'Doe');
    const getUsers = async () => {
        try {
            const users = await User.findAll();
            console.log('Users:', users.map((user) => user.toJSON()));
        } catch (error) {
            console.error('Error fetching users:', error);
        }
    };

// Example usage:
//     getUsers();
    const updateUser = async (userId, newFirstName, newLastName) => {
        try {
            const user = await User.findByPk(userId);
            if (user) {
                await user.update({ firstName: newFirstName, lastName: newLastName });
                console.log('User updated:', user.toJSON());
            } else {
                console.log('User not found');
            }
        } catch (error) {
            console.error('Error updating user:', error);
        }
    };

// Example usage:
    updateUser(1, 'UpdatedJohn', 'UpdatedDoe');

    const deleteUser = async (userId) => {
        try {
            const user = await User.findByPk(userId);
            if (user) {
                await user.destroy();
                console.log('User deleted:', user.toJSON());
            } else {
                console.log('User not found');
            }
        } catch (error) {
            console.error('Error deleting user:', error);
        }
    };
    const getUserById = async (userId) => {
        try {
            const user = await User.find(userId);
            if (user) {
                console.log('User found:', user.toJSON());
            } else {
                console.log('User not found');
            }
        } catch (error) {
            console.error('Error fetching user:', error);
        }
    };

// Example usage:
    getUserById(1); // Assuming 1 is the user ID you want to retrieve

// Example usage:
//     deleteUser(1);

}
main()
// Sync the model with the database

